package com.ninetaildemonfox.zdl.socialproject.utils.dialog.presenter;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;

import com.google.gson.JsonObject;
import com.hyphenate.easeui.utils.map.JSONUtils;
import com.ninetaildemonfox.zdl.ninetaildemonfox.utils.AppManager;
import com.ninetaildemonfox.zdl.socialproject.config.Constants;
import com.ninetaildemonfox.zdl.socialproject.http.https.Model;
import com.ninetaildemonfox.zdl.socialproject.mvp.contract.FlowContract;
import com.ninetaildemonfox.zdl.socialproject.utils.ToastUtil;
import com.ninetaildemonfox.zdl.socialproject.utils.dialog.FlowDialog;

import java.util.ArrayList;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 创建人： Nine tails fox
 * 创建时间： 2019/8/16
 * 功能描述：
 * 联系方式：1037438704@qq.com
 *
 * @author NineTailDemonFox
 */

public class FlowPresenter {
    public AppCompatActivity me;
    public FlowContract contract;

    public FlowPresenter(AppCompatActivity me, FlowContract contract) {
        this.me = me;
        this.contract = contract;
    }

    public void commentList(String token, String member_id) {
        contract.showLoading();
        Model.getInstance().getUserApi().commentList(token, member_id)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ArrayList<Map<String, String>> list = JSONUtils.parseKeyAndValueToMapList(map.get("data"));
                        contract.flowSuccess(list);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

    public void addComment(String token, String cover_member_id, String labels) {
        contract.showLoading();
        Model.getInstance().getUserApi().addComment(token, cover_member_id, labels)
                .enqueue(new Callback<JsonObject>() {
                    @Override
                    public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                        Map<String, String> map = Constants.codeGet(me, response);
                        if (map == null) {
                            return;
                        }
                        ToastUtil.show(map.get("message"));
                        AppManager.getInstance().killActivity(FlowDialog.class);
                    }

                    @Override
                    public void onFailure(Call<JsonObject> call, Throwable t) {

                    }
                });
    }

}
